<?
//<meta http-equiv="content-type" content="text/html; charset=utf-8">
include "head.php";
include_once "umls_toolbox.php";

if ( $_REQUEST[query_dstr]) {
	$query = sprintf("select cui from {$table_prefix}concept where str = '%s'", add_to_query($_REQUEST[query_dstr])); 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	$row = mysql_fetch_array($result);
	$_REQUEST[query_bcui] = $row['cui'];
}

if ( !isset($_REQUEST[query_limit]) ) $_REQUEST[query_limit] = $concept_viewer_default_value['limit'];
if ( !isset($_REQUEST[query_depth]) ) $_REQUEST[query_depth] = $concept_viewer_default_value['depth'];
?>
<p><a href="index.php">HOME</a> / <strong>Concept Viewer</strong></p>
<p align="right">[Ctrl + Click] open new window [Alt + Click] add to temporary</p>
<style type="text/css">
<!--
.expand_default {font-weight:bold;}
.expand_added {color: #0000FF}
.expand_expanded {}
.a{font-weight:normal;}
-->
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script language="javascript">
function get_parent_element(e, tagname)
{
	var c = e;
	while ( true ) {
		if ( c.nodeName == tagname ) return c;
		else if ( c == null ) return null;
		else c = c.parentNode;
	}
	return null;
}

function tree_append_url(expand_type)
{
	var append = '';
	if ( document.form1.show_rel.checked ) append += '&show_rel=1';
	if ( document.form1.show_sab.checked ) append += '&show_sab=1';
	if ( document.form1.show_sty.checked ) append += '&show_sty=1';
	if ( document.form1.show_str.checked ) append += '&show_str=1';
	if ( document.form1.show_cui.checked ) append += '&show_cui=1';
	if ( document.form1.query_limit.value ) append += '&query_limit='+document.form1.query_limit.value;
	if ( document.form1.query_depth.value ) append += '&query_depth='+document.form1.query_depth.value;
	if ( expand_type ) append += '&expand_type='+expand_type; 
	return append;
}

function tree_expand(e, expand_type)
{	
	var eLI = get_parent_element(e, "LI");
	if ( eLI == null ) return true;

	var append = tree_append_url(expand_type);
	var rcui = eLI.id;
	if ( rcui == 'TOP' ) rcui = '';
	var url = 'concept_viewer_get.php?cmd=get&query_table=<?=$_REQUEST[query_table]?>&query_subset=<?=$_REQUEST[query_subset]?>&query_bcui=<?=$_REQUEST[query_bcui]?>&query_rcui='+escape(rcui)+append;
	
	$.getJSON(url, {}, function(json) {
		$("#"+json.id).html(json.html);
		updateShow();
	});
}

function tree_on_dbl_click(e)
{
	if ( !e.altKey ) return true;

	return false;
}

function tree_on_click(e)
{	
	if ( !e.altKey && !e.ctrlKey ) return true;

	var eLI = get_parent_element(getSourceElement(e), "LI");
	if ( eLI == null ) return true;
	
	if ( e.altKey ) {
		add_to_temporary(eLI);

	} else if ( e.ctrlKey ) {
	
		var query_bcui = eLI.attributes['name'].value;
		var append = tree_append_url("");
		var url = "concept_viewer.php?query_table=<?=$_REQUEST[query_table]?>&query_subset=<?=$_REQUEST[query_subset]?>&query_bcui="+query_bcui+append;
		
		window.open(url, "_blank", "");
	}
	return false;
}

function add_to_temporary(eLI)
{
	var eSTR = document.getElementById(eLI.id+'|str');
	if ( eSTR == null ) return;
	
	var cui = eLI.attributes['name'].value;
	if ( eSTR.style.fontWeight == "bold" ) {
		$.getJSON("concept_viewer_process.php", {cmd:'delete', cui:cui}, function(data) {
			eSTR.style.fontWeight = "";
		});
	} else {
		$.getJSON("concept_viewer_process.php", {cmd:'insert', cui:cui}, function(data) {
			eSTR.style.fontWeight = "bold";
		});
	}
}

/*
http://www.quirksmode.org/compatibility.html
http://balanagaraj.wordpress.com/2008/05/07/javascript-differences-in-mozilla-firefox-and-internet-explorer/
*/
function getText(control)
{
	if (document.all) control.innerText;
    else return control.textContent;
}

function setText(control, value)
{
	if (document.all) control.innerText = value;
	else control.textContent = value;
}

function getSourceElement(e)
{
	if (document.all) return e.srcElement;
	return e.target;
}

function fireClickEvent(control)
{
	if (document.all) {
        control.fireEvent("onclick");
    } else {
		var clickEvent = window.document.createEvent("MouseEvent");
        clickEvent.initEvent("click", false, true);
        control.dispatchEvent(clickEvent);
    }
}

function updateShow()
{
	$("#show_rel").change();
	$("#show_sab").change();
	$("#show_sty").change();
	$("#show_str").change();
	$("#show_cui").change();
}

$(document).ready(function(e) {
    
	$("#show_rel").change(function(e) {
		if ( $(this).is(":checked") ) {
			$(".show_rel").show();
		} else {
			$(".show_rel").hide();
		}
	});
	$("#show_sab").change(function(e) {
		if ( $(this).is(":checked") ) {
			$(".show_sab").show();
		} else {
			$(".show_sab").hide();
		}
	});
	$("#show_sty").change(function(e) {
		if ( $(this).is(":checked") ) {
			$(".show_sty").show();
		} else {
			$(".show_sty").hide();
		}
	});
	$("#show_str").change(function(e) {
		if ( $(this).is(":checked") ) {
			$(".show_str").show();
		} else {
			$(".show_str").hide();
		}
	});
	$("#show_cui").change(function(e) {
		if ( $(this).is(":checked") ) {
			$(".show_cui").show();
		} else {
			$(".show_cui").hide();
		}
	});
	
	updateShow();
	
});

</script>
<p>
<form name="form1" method="post" action="<?=$_SERVER[PHP_SELF]?>">
<input type="hidden" name="query_table" value="<?=$_REQUEST[query_table]?>">
<input type="hidden" name="query_subset" value="<?=$_REQUEST[query_subset]?>">
<input type="hidden" name="view_parent" value="<?=$_REQUEST[view_parent]?>">
<input type="hidden" name="view_subtree" value="<?=$_REQUEST[view_subtree]?>">
<table border="0" width="100%" cellspacing="3">
<tr>
  <td><strong>CUI</strong></td>
  <td><input name="query_bcui" type="text" size="6" maxlength="8" value="<?=$_REQUEST[query_bcui]?>">
  <input type="submit" name="Submit" value="Submit">
</td>
  <td align="right"><strong>[Show]</strong>
	<input type="checkbox" id="show_rel" name="show_rel" value="1" <? if ( $_REQUEST[show_rel] ) echo "checked" ?>> REL
	<input type="checkbox" id="show_sab" name="show_sab" value="1" <? if ( $_REQUEST[show_sab] ) echo "checked" ?>> SAB
	<input type="checkbox" id="show_sty" name="show_sty" value="1" <? if ( $_REQUEST[show_sty] ) echo "checked" ?>> STY
	<input type="checkbox" id="show_str" name="show_str" value="1" <? if ( $_REQUEST[show_str] ) echo "checked" ?>> STR
	<input type="checkbox" id="show_cui" name="show_cui" value="1" <? if ( $_REQUEST[show_cui] ) echo "checked" ?>> CUI 

&nbsp;<strong>[Limit]</strong>
  <select name="query_limit">
    <?
	print_option("100", "100", $_REQUEST[query_limit]);
	print_option("200", "200", $_REQUEST[query_limit]);
	print_option("400", "400", $_REQUEST[query_limit]);
	print_option("1000", "1000", $_REQUEST[query_limit]);
  ?>
  </select>
&nbsp;<strong>[Depth]</strong>
  <select name="query_depth">
    <?
	print_option("1", "+1", $_REQUEST[query_depth]);
	print_option("2", "+2", $_REQUEST[query_depth]);
	print_option("3", "+3", $_REQUEST[query_depth]);
	print_option("4", "+4", $_REQUEST[query_depth]);
	print_option("5", "+5", $_REQUEST[query_depth]);
	print_option("6", "+6", $_REQUEST[query_depth]);
	print_option("7", "+7", $_REQUEST[query_depth]);
	print_option("8", "+8", $_REQUEST[query_depth]);
	print_option("9", "+9", $_REQUEST[query_depth]);
  ?>
  </select>
levels</td>
  </tr>
</table>
</form>
</p>
<?
if ( $view_parent != "disable" && $_REQUEST[query_bcui] ) {
	echo "\n<p>Parent view</p>";
	$parent_tree = get_parent_tree($_REQUEST[query_bcui]);
	echo make_parent_tree_html($parent_tree);
}

if ( $view_subtree != "disable" ) {

	echo "\n<p>Child view</p>";
	$sub_tree = get_sub_tree($_REQUEST[query_bcui], $_REQUEST[query_bcui]);
	if ( count($sub_tree) ) {
		echo make_sub_tree_html(array($_REQUEST[query_bcui]=>$sub_tree), true);
	}
}
?>
<p>&nbsp;</p>
<?
include "tail.php";
?>